package sphere.controller;

import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

import sphere.model.Sphere;
import sphere.view.SphereWindow;

public class SphereController implements TextListener
{

	private Sphere model;
	private SphereWindow view;

	public SphereController()
	{
		model = new Sphere(1);
	}

	public SphereController(double rayon)
	{
		model = new Sphere(rayon);
	}

	public SphereController(Sphere fsphere)
	{
		model = fsphere;
	}

	@Override
	public void textValueChanged(TextEvent e)
	{

		TextField t = (TextField) e.getSource();
		double rayon = 0;
		double surface = 0;
		double volume = 0;

		try
		{
			rayon = Double.parseDouble(t.getText());

		} catch (NumberFormatException excep)
		{
			if (t.getText().equals("0"))
			{
				t.setText(null);
			}
			System.out.println("Veuillez entrer un nombre");
		}

		if (t.getText().isEmpty())
		{
			this.view.setrayon(0);
		}

		this.model.setRayon(rayon);
		surface = model.calculerSurface();
		volume = model.calculerVolume();

		this.view.setSurface(surface);
		this.view.setVolume(volume);

	}

	public void setView(SphereWindow sw)
	{
		this.view = sw;
	}

}
